package ch.fhnw.qualiscan.application.pages.tabs;

import java.util.Date;
import java.util.List;
import java.util.Vector;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.modals.DeleteSurvey;
import ch.fhnw.qualiscan.application.pages.EditSurvey;
import ch.fhnw.qualiscan.core.db.fieldfactories.SurveyFieldFactory;
import ch.fhnw.qualiscan.core.db.pojos.Survey;
import ch.fhnw.qualiscan.core.db.pojos.SurveyUserRole;
import ch.fhnw.qualiscan.core.db.pojos.User;
import ch.fhnw.qualiscan.core.helpers.EmailHelper;
import ch.fhnw.qualiscan.core.libraries.NotificationLib;

import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;

import eu.livotov.tpt.i18n.TM;

/**
 * This tab contains the options to edit and delete a survey.
 */
@SuppressWarnings("serial")
public class EditSurveyTab3Coordinator extends VerticalLayout {
	
	private EditSurvey editSurvey;
	private Form surveyForm;
	private BeanItem<Survey> item;
	private SurveyFieldFactory sff;
	private Survey survey;
	
	private Panel p1;
	private Panel p2;
	
	private Date date;
	
	public EditSurveyTab3Coordinator(EditSurvey editSurvey) {
		this.editSurvey = editSurvey;
	
		p1 = new Panel();
		p2 = new Panel();
		
		buildLayout();
	}
	
	public void buildLayout() {
		addComponent(buildSurveyForm());
		addComponent(buildSurveyDelete());
	}
	
	private Panel buildSurveyForm() {
		surveyForm = new Form();
        
		surveyForm.setCaption(TM.get("editsurvey.tab3.title"));
		surveyForm.setDescription(TM.get("editsurvey.tab3.desc"));
		
		survey = (Survey) editSurvey.hibernateSession.getSession().get(Survey.class, editSurvey.currentSurvey.getId());
		this.date = survey.getCompletiondate();
		
		item = new BeanItem<Survey>(survey);
      
        surveyForm.setItemDataSource(item);
        sff = new SurveyFieldFactory();
        surveyForm.setFormFieldFactory(sff);
        
        Vector<String> order = new Vector<String>();
        order.add("title");
        order.add("completiondate");
        
        surveyForm.setVisibleItemProperties(order);
        
        HorizontalLayout buttonBar = new HorizontalLayout();
        surveyForm.getFooter().addComponent(buttonBar);
        
        Button saveButton = new Button(TM.get("action.save"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				try {
					surveyForm.commit();
					editSurvey.surveyModel.updateSurvey(survey);
					editSurvey.buildLayout();
					NotificationLib.showSuccess(TM.get("action.saved"), null);
					
					// date changed, inform participants
					if(survey.getCompletiondate() != date) {
						EmailHelper eH = new EmailHelper();
						List<SurveyUserRole> surList = editSurvey.surveyModel.getParticipantsOfSurvey(survey);
						for(SurveyUserRole sur : surList) {
							eH.sendSurveyCompletionDateChanged(sur.getUser(), survey);
						}
					}
				}
				catch (InvalidValueException e) {
                    return;
				}
			}
		});
        saveButton.setIcon(Config.ICON_SAVE);
        
        HorizontalLayout buttonWrap = new HorizontalLayout();
        buttonWrap.setSpacing(true);
        
        VerticalLayout formWrap = new VerticalLayout();
        formWrap.setSpacing(true);
        
        buttonWrap.addComponent(saveButton);
        
        // if completion date is over
        if(survey.getCompletiondate().compareTo(new Date()) < 0) {
        	HorizontalLayout dateNotice = new HorizontalLayout();
        	dateNotice.setMargin(true);
        	dateNotice.setStyleName("box-warning");
        	dateNotice.addComponent(new Label(TM.get("warning.completiondate")));
        	formWrap.addComponent(dateNotice);
        }
        
        formWrap.addComponent(surveyForm);
        formWrap.addComponent(buttonWrap);
        
        p1.addComponent(formWrap);
        
		return p1;
	}
	
	private Panel buildSurveyDelete() {
		VerticalLayout deleteSurveyWrap = new VerticalLayout();
		deleteSurveyWrap.setSpacing(true);
		
		Label deleteSurvey = editSurvey.textHelper.p(TM.get("editsurvey.tab3.p2.delete"));
		
		Button deleteButton = new Button(TM.get("action.delete"));
		deleteButton.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				editSurvey.openModal(new DeleteSurvey(editSurvey.currentSurvey));
			}
		});
		deleteButton.setIcon(Config.ICON_DELETE);
		
		deleteSurveyWrap.addComponent(deleteSurvey);
		deleteSurveyWrap.addComponent(deleteButton);
		
		p2.addComponent(deleteSurveyWrap);
		
		return p2;
	}
}
